from common.base import Base, get_driver
from selenium.webdriver.common.by import By
from page.index_page import IndexPage
from page.product1_page import Product1Page
from page.product2_page import Product2Page
import time


class ShopPage(Base):
    url = 'http://localhost:8080/ecshop/index.php'

    # 查看购物车
    def check_shop(self):
        # self.click((By.LINK_TEXT, '购物车'))
        self.click((By.XPATH, '//div[@id="ECS_CARTINFO"]/a'))

    # 更新购物车
    def update_shop(self):
        self.click((By.NAME, 'submit'))

    # 清空购物车
    def clear_shop(self):
        self.click((By.CLASS_NAME, 'bnt_blue_1'))

    # 修改商品数量
    def alter_shop(self, content):
        self.find_element((By.XPATH, '//table[@align]//tr[3]//input')).clear()
        self.send_keys((By.XPATH, '//table[@align]//tr[3]//input'), content)
        # self.update()

    # 查看商品数量
    def shop_num(self):
        time.sleep(3)
        return self.get_attribute((By.XPATH, '//table[@align]//tr[3]//input'), 'value')

    # 删除购物车里商品
    def delete_shop(self):
        self.click((By.LINK_TEXT, '删除'))
        confirm = self.driver.switch_to.alert
        confirm.accept()

    # 从购物车回到首页
    def index_shop(self):
        time.sleep(3)
        self.click((By.XPATH, '/html/body/div[2]/div/div[1]/a/img'))

    # 查看购物车商品数量
    def shop_text(self):
        time.sleep(3)
        return self.get_text((By.XPATH, '//a[@title="查看购物车"]'))


if __name__ == '__main__':
    driver = get_driver()
    index = IndexPage(driver)
    product1 = Product1Page(driver)
    product2 = Product2Page(driver)
    shop = ShopPage(driver)
    driver.get('http://localhost:8080/ecshop/index.php')
    # driver.get('http://localhost:8080/ecshop/flow.php?step=cart')
    index.click_shop1()
    product1.click_add()
    shop.index_shop()
    index.click_shop2()
    product2.click_add()
    print(shop.shop_text())
    shop.alter_shop(3)
    shop.update_shop()
    print(shop.shop_num())
    shop.delete_shop()
    # shop.delete()
    shop.clear_shop()
    shop.check_shop()

    # index.quit(10)


